/*
                             *******************
******************************* C SOURCE FILE *******************************
**                           *******************                           **
**                                                                         **
** project		: General Template                                         **
** filename		: SERVER.C                                                 **
** version		: 1.0                                                      **
** date			: December 31, 1986                                        **
**                                                                         **
*****************************************************************************
**                                                                         **
** Copyright (c) 2011, $pynarak                                            **
** All rights reserved.                                                    **
**                                                                         **
*****************************************************************************

VERSION HISTORY:
----------------

Version			: 1.0
Date 			: December 31, 1986
Revised by 		: $pynarak
Compiler        : GNU GCC
Description 	: Original version for LAB EMBEDDED SYSTEM 2011.
*/

#define _SERVER_C_SRC

/****************************************************************************/
/**                                                                        **/
/** MODULES USED                                                           **/
/**                                                                        **/
/****************************************************************************/

#include "server.h"

/****************************************************************************/
/**                                                                        **/
/** DEFINITIONS AND MACROS                                                 **/
/**                                                                        **/
/****************************************************************************/

/****************************************************************************/
/**                                                                        **/
/** TYPEDEFS AND STRUCTURES                                                **/
/**                                                                        **/
/****************************************************************************/

/****************************************************************************/
/**                                                                        **/
/** PROTOTYPES OF LOCAL FUNCTIONS                                          **/
/**                                                                        **/
/****************************************************************************/

int string_length(char *str);

/****************************************************************************/
/**                                                                        **/
/** EXPORTED VARIABLES                                                     **/
/**                                                                        **/
/****************************************************************************/

/****************************************************************************/
/**                                                                        **/
/** GLOBAL VARIABLES                                                       **/
/**                                                                        **/
/****************************************************************************/

/****************************************************************************/
/**                                                                        **/
/** EXPORTED FUNCTIONS                                                     **/
/**                                                                        **/
/****************************************************************************/

/****************************************************************************/
/**                                                                        **/
/** LOCAL FUNCTIONS                                                        **/
/**                                                                        **/
/****************************************************************************/

int main(int argc, char *argv[])
{
	/*********************************************
	First argument is file name
	Second argument is server ip address
	**********************************************/
	
	int 	server_sockfd, client_sockfd;
	int 	server_len, client_len;
	struct sockaddr_in server_address;
	struct sockaddr_in client_address;
	int 	result;
	int 	round = 0;
	char 	buf[255];
	int 	loop;
	char 	command[5];
	char	name[20];
	char 	data[255];

	char	*file;
	char	*getData;
	int 	con1=0;
	int 	con2=0;
	size_t	nbytes;
	FILE 	*pFile;
	int	datasize;
	int	datasize2;
	size_t  result2;
	size_t  result3;

		/* Create an unnamed socket for the server. */
		server_sockfd = socket(AF_INET, SOCK_STREAM, 0);

		/* Assign a name to a socket. */
		server_address.sin_family = AF_INET;
		//server_address.sin_addr.s_addr = inet_addr(argv[1]);
		server_address.sin_addr.s_addr = inet_addr("192.168.1.222");
		server_address.sin_port = 2129;
		server_len = sizeof(server_address);
		result = bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
		if(result == -1)
		{
			perror("OOPS!!: bind fnc. error");
			exit(1);
		}
		
		/* Create a connection queue and wait for clients. */
		result = listen(server_sockfd, 5);
		if(result == -1)
		{
			perror("OOPS!!: listen fnc. error");
			exit(1);
		}

		while(1)
		{
			printf("server waiting round: %02d\n", round);
			round++;

			/* Accept a connection. */
			client_len = sizeof(client_address);
			client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_address, &client_len);


			/* Read data from client via sockfd. */
			nbytes = sizeof(buf);
			result = read(client_sockfd, buf, nbytes);
			/* Debug */
			printf("string from client: %s\n", buf);
			if(result == -1)
			{
				perror("OOPS!!: read fnc. error");
			}

			
			for(loop=0;loop<3;loop++)
			{
				switch(loop)
				{
					case 0 :
					

					while((buf[con1]!=' ')&&(buf[con1]!='\0'))
					{
						command[con2] = buf[con1];
						con1++;
						con2++;
						
					}
					
					command[con2] = '\0';
					
					con1++;
					con2=0;
					
					break;
					
					case 1 :
					
					
					while((buf[con1]!=' ')&&(buf[con1]!='\0'))
					{
						name[con2] = buf[con1];
						con1++;
						con2++;
						
					}
					name[con2] = '\0';
					
					con1++;
					con2=0;
					
					break;

					case 2 :
					
					while(buf[con1]!='\0')
					{
						data[con2] = buf[con1];
						con1++;
						con2++;
						
					}
					data[con2] = '\0';
					con1++;
					
					con2=0;
					
					break;
					
					default : break;
				}
				

			}
			con1 = 0;


			if((command[0]=='v')&&(command[1]=='i'))
			{
				

				pFile = fopen(name,"a");
				if(pFile!=NULL)
				{
					fputs(data,pFile);
					fputs("\n\r",pFile);
					fclose(pFile);
				}
				
				pFile = fopen("filename.txt","a");
				if(pFile!=NULL)
				{
					fputs(name,pFile);
					fputs("\n\r",pFile);
					fclose(pFile);
				}
			}
			else if((command[0]=='l')&&(command[1]=='s'))
			{
				pFile = fopen("filename.txt","rb");
				fseek(pFile,0,SEEK_END);
				datasize = ftell(pFile);
				rewind(pFile);
				//printf("%d\n",datasize);
				file = malloc(sizeof(char)*datasize);
			
				result2 = fread(file,1,datasize,pFile);
				//printf("%s\n",file);
				fclose(pFile);
				printf("%s\n",file);
				free(file);
			
				
			}	
			else if((command[0]=='c')&&(command[1]=='a')&&(command[2]=='t'))
			{
				//printf("name = %s\n",name);
				pFile = fopen(name,"rb");
				if(pFile==NULL)perror("Error Openning");
				else
				{
				fseek(pFile,0,SEEK_END);
				datasize2 = ftell(pFile);
				rewind(pFile);
				//printf("%d\n",datasize2);
				getData = malloc(sizeof(char)*datasize2);
			
				result3 = fread(getData,1,datasize2,pFile);
				//printf("%s\n",file);
				fclose(pFile);
				printf("data from %s : %s\n",name,getData);
				free(getData);
				}
			}	
			



			close(client_sockfd);
		}
	
}


int string_length(char *str)
{
	int i = 0;
	
	while (str[i] != 0)
	{
		i++;
	}

	return (i + 1);
}

/****************************************************************************/
/**                                                                        **/
/** EOF                                                                    **/
/**                                                                        **/
/****************************************************************************/
